// pages/certifiedRider/certifiedRider.js
const api = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		arr: [],
		index: '',
		ageIndex: '',
		displayView: 1,
		tempFilePaths: [],
		status: [{
			"status": 1,
			"display": true,
			"statusName": "实名认证"
		}, {
			"status": 2,
			"display": false,
			"statusName": "学生认证"
		}, {
			"status": 3,
			"display": false,
			"statusName": "绑定手机"
		}, {
			"status": 4,
			"display": false,
			"statusName": "完成认证"
		}],
		array: [],
		imgUrl: [],
		ageTow: '',
		arrayYear: [],
		formSubmit: {},
		currentYear: '',
		studentFormSubmit: {},
		phoneFormSubmit: {},
	},
	createdYear() {
		let arrayYear = []
		for (let i = 1900; i <= 2100; i++) {
			arrayYear.push(`${i}年`)
		}
		let index = arrayYear.findIndex((item, index) => {
			return item == `${this.data.currentYear}年`
		})
		console.log(index)
		this.setData({
			arrayYear,
			ageIndex: index
		})
	},
	getSchool() {
		api.fetchApi('procedure_t', {
			p: `R2005003`
		}).then(res => {
			const {
				code,
				results
			} = res.data
			if (code == 1) {
				this.setData({
					array: results,
				})
			} else {
        $Toast({
          content: `${res.data.errorMsg}`,
          type: 'error'
        });
			}
		})
	},
	bindPickerChange(e) {
		this.setData({
			index: e.detail.value
		})
	},
	bindPickerAgeChange(e) {
		let ageTow = this.data.arrayYear[e.detail.value]
		console.log(ageTow)
		this.setData({
			ageTow
		})
	},
	formSubmit(e) {
		console.log(e.detail.value)
		let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
		if (!e.detail.value.name.trim()) {
      $Toast({
        content: '请输入姓名',
        type: 'warning'
      });
		} else if (!(reg.test(e.detail.value.idcard))) {
      $Toast({
        content: '请输入有效的身份证号',
        type: 'warning'
      });
		} else if (!this.data.tempFilePaths[0]) {
      $Toast({
        content: '请上传身份证号照片',
        type: 'warning'
      });
		} else {
			this.data.formSubmit = e.detail.value
			this.nextStep()
		}
	},
	studentFormSubmit(e) {
		console.log(e.detail.value)
		if (!e.detail.value.number.trim()) {
      $Toast({
        content: '请填写您的学号',
        type: 'warning'
      });
		} else if (!e.detail.value.school.trim()) {
		
      $Toast({
        content: '请选择所在学校',
        type: 'warning'
      });
		} else if (!e.detail.value.series.trim()) {
      $Toast({
        content: '请填写所在院系',
        type: 'warning'
      });
		} else if (!e.detail.value.major.trim()) {
      $Toast({
        content: '请填写就读专业',
        type: 'warning'
      });
		} else if (!e.detail.value.yearAdmission.trim()) {
      $Toast({
        content: '请选择入校年份',
        type: 'warning'
      });
		} else if (!this.data.tempFilePaths[1]) {
      $Toast({
        content: '请上传身份证号照片',
        type: 'warning'
      });
		} else {
			this.data.studentFormSubmit = e.detail.value
			this.nextStep()
		}
	},
	phoneFormSubmit(e) {
		console.log(e.detail.value)
		let reg = /^1[3456789]\d{9}$/;
		if (!(reg.test(e.detail.value.phone))) {
      $Toast({
        content: '请输入有效的手机号码',
        type: 'warning'
      });
		} else {
			this.data.phoneFormSubmit = e.detail.value
			if (this.data.displayView == 3) {
				this.submitCertified()
			}
		}
	},
	uploadIdentity(e) {
		let id = e.currentTarget.dataset.id
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['camera'],
			success: (res) => {
				let tempFilePaths = res.tempFilePaths
				this.data.arr[id] = tempFilePaths
				wx.uploadFile({
					url: `${app.globalData.host}file`,
					filePath: tempFilePaths[0],
					name: 'file',
					success: (res) => {
						let data = JSON.parse(res.data)
						if (data.code == 1) {
							this.data.imgUrl[id] = data.url
							this.setData({
								tempFilePaths: this.data.arr,
							})
						} else {
              $Toast({
                content: '上传失败',
                type: 'error'
              });
						}
					}
				})
			}
		})
	},
	back() {
		let displayView = this.data.displayView - 1
		let display = "status[" + displayView + "].display";
		this.setData({
			[display]: false,
			displayView: displayView
		})
	},
	nextStep() {
		let displayView = this.data.displayView + 1
		let display = "status[" + this.data.displayView + "].display";
		this.setData({
			[display]: true,
			displayView: displayView
		})
	},
	submitCertified() {
		let uid = app.globalData.uid;
		let imgUrl = this.data.imgUrl
		let formSubmit = this.data.formSubmit
		let studentInfo = this.data.studentFormSubmit
		let phoneFormSubmit = this.data.phoneFormSubmit
		api.fetchApi('procedure_t', {
			p: `R2005005|${uid}|${formSubmit.name}|${formSubmit.idcard}|${imgUrl[0]}|${imgUrl[1]}|${phoneFormSubmit.phone}|${studentInfo.number}|${studentInfo.school}|${studentInfo.yearAdmission}|${studentInfo.series}|${studentInfo.major}`
		}).then(res => {
			const {
				code,
				results
			} = res.data
			if (code == 1) {
				this.nextStep()
				setTimeout(() => {
					wx.navigateBack()
				}, 1500)
			} else {
        $Toast({
          content: `${res.data.errorMsg}`,
          type: 'error'
        });
			}
		})
	},
	isSubmit(){
		let uid = app.globalData.uid;
		api.fetchApi('procedure_t', {
			p: `R2011010|${uid}`
		}).then(res => {
			const {code, results} = res.data
			if (code == 1) {
				if (results[0].codes == 2) {
					let display = "status[3].display";
					let init = "status[0].display";
					this.setData({
						[init]: false,
						[display]: true,
						displayView: 4
					})
				}
			} else {
        $Toast({
          content: `${res.data.errorMsg}`,
          type: 'error'
        });
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let currentDate = new Date();
		let currentYear = currentDate.getFullYear()
		this.setData({
			currentYear
		})
		// console.log(currentYear)
		this.isSubmit()
		this.getSchool()
		this.createdYear()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
